<?php

namespace addons\csmitsm\library\xcore\xcore\utils;

use fast\Pinyin;
use addons\csmitsm\library\xcore\xcore\utils\XcDaoUtils;
use addons\csmitsm\library\xcore\xcore\utils\XcConfigUtils;
use addons\csmitsm\library\sdnode\xstack\utils\XsStringUtils;
use addons\csmitsm\library\xcore\xcore\utils\XcDatabaseUtils;
use addons\csmitsm\library\xcore\xcore\utils\XcIncludeFileUtils;


class DevXcFaUtils
{

    public static function generate_install_sql($viewsql='')
    {
        
        $filename = XcIncludeFileUtils::getAddonsPath("install.sql");

        if (true) {
            $list = XcDatabaseUtils::queryTableList("fa_csmitsm", [], false);

            $tablenames = XcDaoUtils::getValuesFromList($list, "name");
            $tablenamestrings = XsStringUtils::join($tablenames, " ");

            $database_username = XcConfigUtils::databaseconfig("username");
            $database_password = XcConfigUtils::databaseconfig("password");
            $database_database = XcConfigUtils::databaseconfig("database");

            $command_config = "--skip-add-locks --skip-comments --compact --skip-add-drop-table --skip-add-locks  --skip-add-drop-table";

            $command = "mysqldump -u{$database_username} -p{$database_password} {$database_database} {$command_config} --tables {$tablenamestrings}> {$filename}";
            exec($command, $output, $return_var);
            var_dump($command, $output, $return_var);
        }
        if (true) {
            $originalContent = file_get_contents($filename);
            $originalContent .= $viewsql;

            $replace = "__PREFIX__";
            $search = "fa_";
            $originalContent = preg_replace("/" . preg_quote($search, "/") . "/i", $replace, $originalContent);

            $originalContent = XsStringUtils::replace($originalContent, "utf8mb4_unicode_ci", "utf8mb4_general_ci");
            $originalContent = XsStringUtils::replace($originalContent, "CREATE TABLE", "CREATE TABLE IF NOT EXISTS");

            $finalContent = 'SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
' . $originalContent . '
COMMIT;
            ';
            file_put_contents($filename, $finalContent);
        }
    }
    /**
     * add_menu_function(1, "xcflow", [["mdmconfig", "配置"]]);
     * @return void
     */
    public static function add_menu_function($pid, $controlpath, $new_menus)
    {
        $new_menus[] = ["index","查询"];
        $new_menus[] = ["add","创建"];
        $new_menus[] = ["edit","修改"];
        $new_menus[] = ["del","删除"];
        $new_menus[] = ["multi","批量"];


        $dao = new \app\admin\model\AuthRule();
        foreach ($new_menus as $new) {
            $new_name = XcConfigUtils::getAddonCode() . "/{$controlpath}/" . $new[0];
            $row = XcDaoUtils::getRowBySql($dao, [['name', '=', $new_name]]);
            if ($row == null) {
                $row = XcDaoUtils::create($dao, [
                    "type" => "file",
                    "pid" => $pid,
                    "name" => $new_name,
                    "title" => $new[1],
                    "icon" => "fa fa-circle-o",
                    "ismenu" => 0,
                    'status' => 'normal',
                    'py' => Pinyin::get($new[1], true),
                    'pinyin' => Pinyin::get($new[1]),
                    "weigh" => 0,
                ]);

                pp("创建菜单: #{$row->id} {$new[0]}");
            } else {
                pp("菜单已经存在, 跳过: #{$row->id} {$new[0]}");
            }
        }
    }
}
